using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QFramework;

// 基类接口
public class ILiveData
{
    public string msg_id;
    public string sec_openid;
    public string avatar_url;
    public string nickname;
    public long timestamp;
    public GameUserData game_user;
}

[System.Serializable]
public class GameUserData
{
    public int id;
    public int level;

    public string avatar_url;

    public string name;
    public int liansheng;
    public int score;
    public int success_nums;
    public int total_nums;
}

[System.Serializable]
public class LiveCommentData:ILiveData
{
    public string content;

}

[System.Serializable]
public class LiveGiftData:ILiveData
{

    public string sec_gift_id;
    public int gift_num;
    public int gift_value;
    public bool test;

}

[System.Serializable]
public class LiveLikeData:ILiveData
{
    public int like_num;

}

[System.Serializable]
public class RoundSettlementData
{
    public long round_id;
}

[System.Serializable]
public class LiveMessage
{
    public string cmd;
    public List<ILiveData> data;
    public long timestamp;
    public string msg_id;
}

public class LiveMessageComment
{
    public string cmd;
    public List<LiveCommentData> data;
    public long timestamp;
    public string msg_id;
}

public class LiveMessageGift
{
     public string cmd;
    public List<LiveGiftData> data;
    public long timestamp;
    public string msg_id;
}

public class LiveMessageLike
{
     public string cmd;
    public List<LiveLikeData> data;
    public long timestamp;
    public string msg_id;
}

public class LiveMessageRoundSettlement
{
    public string cmd;
    public RoundSettlementData data;
    public long timestamp;
    public string msg_id;
}

[System.Serializable]
public class JoinCampData
{
    public int group_id;
    public string group_name;
    public GameUserData game_user;
}

public class LiveMessageJoinCamp
{
    public string cmd;
    public JoinCampData data;
    public long timestamp;
    public string msg_id;
}

[System.Serializable]
public class UserUpgradeData
{
    public int id;
    public int level;
}

public class LiveMessageUserUpgrade
{
    public string cmd;
    public UserUpgradeData data;
    public long timestamp;
    public string msg_id;
}

public class ReceiveSocket : MonoBehaviour
{
    // 定义事件
    public event Action<LiveCommentData> OnLiveCommentReceived;
    public event Action<LiveGiftData> OnLiveGiftReceived;

    public event Action<LiveLikeData> OnLiveLikeReceived;

    public event Action<RoundSettlementData> OnRoundSettlementReceived;

    public event Action<JoinCampData> OnJoinCampReceived;

    void Start()
    {
        WebSocketManager.Instance.OnMessageReceived += OnMessageReceived;
    }

    // Update is called once per frame
    private void OnMessageReceived(string message)
    {
        Debug.Log("Received message: " + message);
            // 先解析基本信息
            var jObj = JObject.Parse(message);
            string cmd = jObj["cmd"]?.ToString();
            // 处理具体消息
            switch (cmd)
            {
                case "live_comment":
                    var liveMessageComment = JsonConvert.DeserializeObject<LiveMessageComment>(message);
                    foreach (var item in liveMessageComment.data)
                    {
                        if (item is LiveCommentData comment)
                        {
                            Debug.Log($"收到评论: {comment.nickname}: {comment.content} 等级:{comment.game_user?.level}");
                            OnLiveCommentReceived?.Invoke(comment);
                             // 触发消息接收事件，用于上报
                            string msgId = comment.msg_id;
                            if (!string.IsNullOrEmpty(cmd) && !string.IsNullOrEmpty(msgId))
                            {
                                StringEventSystem.Global.Send(EventID.SocketMessageReceived, new object[] { cmd, msgId });
                            }
                        }
                    }
                    break;

                case "live_gift":
                    var liveMessageGift = JsonConvert.DeserializeObject<LiveMessageGift>(message);
                    foreach (var item in liveMessageGift.data)
                    {
                        if (item is LiveGiftData gift)
                        {
                            Debug.Log($"原始 JSON: {JsonConvert.SerializeObject(gift)}");
                            Debug.Log($"礼物个数：{gift.gift_num}");
                            Debug.Log($"礼物金额：{gift.gift_value}");
                            Debug.Log($"收到礼物: {gift.nickname} 赠送了 {gift.gift_num} 个礼物，价值 {gift.gift_value}");
                            OnLiveGiftReceived?.Invoke(gift);
                             // 触发消息接收事件，用于上报
                             string msgId = gift.msg_id;
                            if (!string.IsNullOrEmpty(cmd) && !string.IsNullOrEmpty(msgId))
                            {
                                StringEventSystem.Global.Send(EventID.SocketMessageReceived, new object[] { cmd, msgId });
                            }
                        }
                    }
                    break;

                case "live_like":
                    var liveMessageLike = JsonConvert.DeserializeObject<LiveMessageLike>(message);
                    foreach (var item in liveMessageLike.data)
                    {
                        if (item is LiveLikeData like)
                        {
                            Debug.Log($"收到点赞: {like.nickname} 点赞了 {like.like_num} 次");
                            OnLiveLikeReceived?.Invoke(like);
                             // 触发消息接收事件，用于上报
                             string msgId = like.msg_id;
                            if (!string.IsNullOrEmpty(cmd) && !string.IsNullOrEmpty(msgId))
                            {
                                StringEventSystem.Global.Send(EventID.SocketMessageReceived, new object[] { cmd, msgId });
                            }
                        }
                    }
                    break;

                case "roundSettlement":
                    var roundSettlement = JsonConvert.DeserializeObject<LiveMessageRoundSettlement>(message);
                    if (roundSettlement?.data != null)
                    {
                        Debug.Log($"收到回合结算: round_id = {roundSettlement.data.round_id}");
                        OnRoundSettlementReceived?.Invoke(roundSettlement.data);
                    }
                    break;

                case "join_camp":
                    var joinCamp = JsonConvert.DeserializeObject<LiveMessageJoinCamp>(message);
                    if (joinCamp?.data != null)
                    {
                        Debug.Log($"收到加入阵营: 用户 {joinCamp.data.game_user.name} 加入了 {joinCamp.data.group_name}(ID: {joinCamp.data.group_id})");
                        OnJoinCampReceived?.Invoke(joinCamp.data);
                    }
                    break;

                case "userUpgrade":
                    var userUpgrade = JsonConvert.DeserializeObject<LiveMessageUserUpgrade>(message);
                    if (userUpgrade?.data != null)
                    {
                        Debug.Log($"用户升级: 用户ID {userUpgrade.data.id} 升级到 {userUpgrade.data.level} 级");
                        StringEventSystem.Global.Send(EventID.SocketUpdateUserLevel, userUpgrade.data.id.ToString());
                    }
                    break;
            
        }
    }

    private void OnDestroy()
    {
        if (WebSocketManager.Instance != null)
        {
            WebSocketManager.Instance.OnMessageReceived -= OnMessageReceived;
        }
    }
}
